$(document).ready(function(){

		
	$(".droppable").droppable({
		accept :		'.draggable', 
		drop:			function (event, ui) 
						{
							$(this).append(ui.draggable);
						},
		tolerance:		'pointer'
		});
		
	$("#createPlaylistBtn").click( function(){
	
		var songs = "";
		
		var items = $("#playlist > *").each(function(i) {
     		songs += $(this).html();
   		});
		
		if( songs == "" ){
			// TODO - Hacer algo bonito.
			return;
		}
		
		
		$.post("/creambomb/createplaylist/", { songs: songs },
		  	function(data){
		  		var html = '<object class="player" type="application/x-shockwave-flash" width="300" height="170"' +
							'data="/media/javascript/mp3player/xspf_player.swf?playlist_url=' + data.url + '" ' +
							'autoplay="True"> <param id="movieParam" name="movie"' +
							'value="/media/javascript/mp3player/xspf_player.swf?playlist_url=' + data.url + '" ' + '/></object>';
		
				var player = jQuery(html);
		  		$('#player').html(html);
				
		  	}, "json");
			
	});
	
	$("#clearPlaylistBtn").click( function(){
	
		var songs = "";

		$.post("/creambomb/clearplaylist/", {},
		  	function(data){
		  		
		  		if (data.result == 'NOPLAYLIST'){
		  			// TODO - Change for sth nice.
		  			alert('No playlist to clear.');	
		  		}
		  		
		  		var html = '<object class="player" type="application/x-shockwave-flash" width="300" height="170"' +
							'data="/media/javascript/mp3player/xspf_player.swf?playlist_url=http://catadesk.no-ip.org:8000/media/playlists/clear.xspf"' +
							'autoplay="True"> <param id="movieParam" name="movie" value="/media/javascript/mp3player/xspf_player.swf?playlist_url=' +
							'http://catadesk.no-ip.org:8000/media/playlists/clear.xspf" /></object>';
		
				var player = jQuery(html);
		  		$('#player').html(html);
		  		
		  	}, "json");
		
	});
	
	$('#queryForm').submit(function(e){
			e.preventDefault();
			search();
	});
	
	$('#searchBtn').click(function(e){
			search();
	});

	$("#nextBtn").click( function(){
		
		loadSongs("")
	});
	
	function search(){
	
		var query = document.getElementById('query').value
		
		$('#nextBtn').css('display', 'none');
		
		loadSongs(query);
		
	}
	
	function loadSongs(query){
		var isFirstCall = 0;
		var resultsList = $("#songsList");
			
		if( query != "" ){
			isFirstCall = 1;
		}
		else if( $('#songsList').html() == "<li>No more songs.</li>" ){
			
			return;
			
		}
		
		$('#songsList').empty().html("<img class='loader' src='/media/images/loader.gif' />");
		
		
		$.post("/creambomb/searchsong/", { song:query, firstCall:isFirstCall },
		  	function(data){
		  		var html = ""

				if( data.result == "NOMORESONGS" ){
					$('#songsList').html("<li>No more songs.</li>");
					$('#nextBtn').css('display', 'none');
					return;
				}
				else if( data.result == 'NORESULTS' ){
					$('#songsList').html("<li>No results found.</li>");
					return;
				}
				else{
					$('#nextBtn').css('display', 'inline');
				}
				
		  		for (var i = 0 ; i < data.length ; i++ ){
		  			html += "<li class='draggable'><song name='url' hidden=true value='" + 
		  			data[i].location + "'>" + data[i].name + " - By " + data[i].artist + " </song></li>";
		  		}
		  		resultsList.html(html);
		  		
		  		$(".draggable").draggable({
					zIndex: 	1000,
					ghosting:	true,
					revert: 	true,
					opacity: 	0.7,
					fx:			200
				});
		  				  		
		  	}, "json");	
	}
	
	$("#clearBtn").click( function(){
		$('#playlist').html("");
	});
	
});